package com.lwj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lwj.dao.TeamMapper;
import com.lwj.dao.UserMapper;
import com.lwj.dao.UserTeamMapper;
import com.lwj.entity.*;
import com.lwj.model.TeamMemberRes;
import com.lwj.model.TeamRes;
import com.lwj.service.TeamService;
import com.lwj.util.DataRet;
import com.sun.deploy.uitoolkit.impl.text.TextAppletAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author linwj
 * @date 2018/5/10 13:32
 */

@Service
@Transactional
public class TeamServiceImpl implements TeamService {

    @Autowired
    private TeamMapper teamMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserTeamMapper userTeamMapper;

    @Override
    public DataRet<String> create(User user, Team team) {
        if (user.getId() != null) {
            team.setStatus("0");
            team.setAlipay(team.getAlipay());
            teamMapper.insert(team);
            UserTeam userTeam = new UserTeam();
            userTeam.setRole(1);
            userTeam.setUserId(user.getId());
            userTeam.setTeamId(team.getId());
            userTeamMapper.insert(userTeam);
            return null;
        }
        return new DataRet<>("创建失败");
    }

    @Override
    public DataRet<String> invitationMember(User user, int teamId) {
        user = userMapper.selectByUser(user);
        if (user != null) {
            user.setTeamId(teamId);
            userMapper.insert(user);
            return null;
        }
        return new DataRet<>("邀请失败");
    }

    @Override
    public PageInfo<TeamRes> get(User user, int pageNo, int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        return new PageInfo<>(teamMapper.getTeam(user));
    }

    @Override
    public DataRet<List<Team>> getUserLeaderTeam(User user) {
        return new DataRet<>(teamMapper.getUserLeaderTeam(user));
    }

    @Override
    public DataRet<String> userTeamValidation(int userId, int teamId) {
        UserTeam userTeam = new UserTeam();
        userTeam.setUserId(userId);
        userTeam.setTeamId(teamId);
        userTeam = userTeamMapper.getByUserTeam(userTeam);
        if (userTeam != null) {
            return new DataRet<>("true");
        } else {
            return new DataRet<>("false");
        }
    }

    @Override
    public DataRet<String> inviteUser(int userId, int teamId) {
        UserTeam userTeam = new UserTeam();
        userTeam.setUserId(userId);
        userTeam.setTeamId(teamId);
        userTeam.setCreateTime(new Date());
        userTeam.setJoinTime(new Date());
        userTeam.setRole(0);
        userTeamMapper.insert(userTeam);
        return new DataRet<>("添加成功");
    }

    @Override
    public DataRet<List<TeamMemberRes>> getTeamMemberInfo(User user, int teamId) {
        List<TeamMemberRes> teamMemberRes = userTeamMapper.getTeamMemberInfo(teamId);
        for (TeamMemberRes t : teamMemberRes
                ) {
            if (t.getId().equals(user.getId())) {
                t.setUserStatus(true);
            }
        }
        return new DataRet<>(teamMemberRes);
    }
}
